package com.coscon.controller;

import com.coscon.controller.base.BaseController;
import com.coscon.model.ResponseResult;
import com.coscon.model.api.ResponseCode;
import com.coscon.model.db.WakeUpConfig;
import com.coscon.service.WakeUpService;
import com.coscon.utils.WakeOnLan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 网络唤醒
 */
@Controller
@RequestMapping("/jm/manager/wakeup")
@Slf4j
public class WakeUpController extends BaseController {

    @Resource
    private WakeUpService wakeUpService;

    @GetMapping("/index")
    public ModelAndView welcomePage(HttpServletRequest request){
        ModelAndView v = new ModelAndView("appPage/wakeUp");
        List<WakeUpConfig> list = this.wakeUpService.getWakeUpConfigList();
        v.addObject("list",list);
        return v;
    }

    /**
     * 保存配置
     * @param request
     * @param hostname
     * @param mac
     * @return
     */
    @PostMapping("/saveWakeUp")
    public @ResponseBody
    ResponseResult<Boolean> saveWakeUp(
            HttpServletRequest request,
            @RequestParam(value = "hostname", required = false) String hostname
           ,  @RequestParam(value = "mac", required = false) String mac
    ){
        boolean f = wakeUpService.saveWakeUpConfig(mac,hostname);
        return new ResponseResult(ResponseCode.OK.getCode(),ResponseCode.OK.getMsg(),f);
    }

    /**
     * 执行唤醒
     * @param request
     * @param mac
     * @return
     */
    @PostMapping("/execWakeUp")
    public @ResponseBody
    ResponseResult<Boolean> execWakeUp(
            HttpServletRequest request
            ,  @RequestParam(value = "mac", required = false) String mac
    ){
        WakeOnLan wl = new WakeOnLan();
        boolean f = wl.wakeByMac(mac);
        return new ResponseResult(ResponseCode.OK.getCode(),ResponseCode.OK.getMsg(),f);
    }

    /**
     * 执行删除
     * @param request
     * @param dataId
     * @return
     */
    @PostMapping("/execDelete")
    public @ResponseBody
    ResponseResult<Boolean> execDelete(
            HttpServletRequest request
            ,  @RequestParam(value = "dataId", required = false) String dataId
    ){
        boolean f = this.wakeUpService.deleteById(dataId);
        return new ResponseResult(ResponseCode.OK.getCode(),ResponseCode.OK.getMsg(),f);
    }



}
